fa88e17088d521e50712ccf3a837ee3c61821d12,tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java,PackageDataFormatMojo,prepareDataFormat,#Log#MavenProject#MavenProjectHelper#File#File#BuildContext#,106
Before Change
File outFile = new File(camelMetaDir, "dataformat.properties");
try {
properties.store(new FileWriter(outFile), "Generated by camel-package-maven-plugin");
buildContext.refresh(outFile);
log.info("Generated " + outFile + " containing " + count + " Camel " + (count > 1 ? "dataformats: " : "dataformat: ") + names);
if (projectHelper != null) {
List<String> includes = new ArrayList<String>();
includes.add("**/dataformat.properties");
projectHelper.addResource(project, dataFormatOutDir.getPath(), includes, new ArrayList<String>());
projectHelper.attachArtifact(project, "properties", "camelDataFormat", outFile);
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
After Change
// first we need to setup the output directory because the next check
// can stop the build before the end and eclipse always needs to know about that directory
if (projectHelper != null) {
projectHelper.addResource(project, dataFormatOutDir.getPath(), Collections.singletonList("**/dataformat.properties"), Collections.emptyList());
}
if (!PackageHelper.haveResourcesChanged(log, project, buildContext, "META-INF/services/org/apache/camel/dataformat")) {
return;
}
Map<String, String> javaTypes = new HashMap<String, String>();
StringBuilder buffer = new StringBuilder();
int count = 0;
for (Resource r : project.getBuild().getResources()) {
File f = new File(r.getDirectory());
if (!f.exists()) {
f = new File(project.getBasedir(), r.getDirectory());
}
f = new File(f, "META-INF/services/org/apache/camel/dataformat");
if (f.exists() && f.isDirectory()) {
File[] files = f.listFiles();
if (files != null) {
for (File file : files) {
// skip directories as there may be a sub .resolver directory
if (file.isDirectory()) {
continue;
}
String name = file.getName();
if (name.charAt(0) != '.') {
count++;
if (buffer.length() > 0) {
buffer.append(" ");
}
buffer.append(name);
}
if (!buildContext.hasDelta(file)) {
// if this file has not changed,
// then no need to store the javatype
// for the json file to be generated again
// (but we do need the name above!)
continue;
}
// find out the javaType for each data format
try {
String text = loadText(new FileInputStream(file));
Map<String, String> map = parseAsMap(text);
String javaType = map.get("class");
if (javaType != null) {
javaTypes.put(name, javaType);
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to read file " + file + ". Reason: " + e, e);
}
}
}
}
}
// find camel-core and grab the data format model from there, and enrich this model with information from this artifact
// and create json schema model file for this data format
try {
if (count > 0) {
Artifact camelCore = findCamelCoreArtifact(project);
if (camelCore != null) {
File core = camelCore.getFile();
if (core != null) {
URL url = new URL("file", null, core.getAbsolutePath());
URLClassLoader loader = new URLClassLoader(new URL[]{url});
for (Map.Entry<String, String> entry : javaTypes.entrySet()) {
String name = entry.getKey();
String javaType = entry.getValue();
String modelName = asModelName(name);
InputStream is = loader.getResourceAsStream("org/apache/camel/model/dataformat/" + modelName + ".json");
if (is == null) {
// use file input stream if we build camel-core itself, and thus do not have a JAR which can be loaded by URLClassLoader
is = new FileInputStream(new File(core, "org/apache/camel/model/dataformat/" + modelName + ".json"));
}
String json = loadText(is);
if (json != null) {
DataFormatModel dataFormatModel = new DataFormatModel();
dataFormatModel.setName(name);
dataFormatModel.setTitle("");
dataFormatModel.setModelName(modelName);
dataFormatModel.setLabel("");
dataFormatModel.setDescription(project.getDescription());
dataFormatModel.setJavaType(javaType);
dataFormatModel.setGroupId(project.getGroupId());
dataFormatModel.setArtifactId(project.getArtifactId());
dataFormatModel.setVersion(project.getVersion());
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("model", json, false);
for (Map<String, String> row : rows) {
if (row.containsKey("title")) {
String title = row.get("title");
dataFormatModel.setTitle(asModelTitle(name, title));
}
if (row.containsKey("label")) {
dataFormatModel.setLabel(row.get("label"));
}
if (row.containsKey("javaType")) {
dataFormatModel.setModelJavaType(row.get("javaType"));
}
// override description for camel-core, as otherwise its too generic
if ("camel-core".equals(project.getArtifactId())) {
if (row.containsKey("description")) {
dataFormatModel.setLabel(row.get("description"));
}
}
}
log.debug("Model " + dataFormatModel);
// build json schema for the data format
String properties = after(json, " \"properties\": {");
String schema = createParameterJsonSchema(dataFormatModel, properties);
log.debug("JSon schema\n" + schema);
// write this to the directory
File dir = new File(schemaOutDir, schemaSubDirectory(dataFormatModel.getJavaType()));
dir.mkdirs();
File out = new File(dir, name + ".json");
try (OutputStream fos = buildContext.newFileOutputStream(out)) {
fos.write(schema.getBytes());
}
log.debug("Generated " + out + " containing JSon schema for " + name + " data format");
}
}
}
}
}
} catch (Exception e) {
throw new MojoExecutionException("Error loading dataformat model from camel-core. Reason: " + e, e);
}
if (count > 0) {
Properties properties = new Properties();
String names = buffer.toString();
properties.put("dataFormats", names);
properties.put("groupId", project.getGroupId());
properties.put("artifactId", project.getArtifactId());
properties.put("version", project.getVersion());
properties.put("projectName", project.getName());
if (project.getDescription() != null) {
properties.put("projectDescription", project.getDescription());
}
camelMetaDir.mkdirs();
File outFile = new File(camelMetaDir, "dataformat.properties");
try {
try(OutputStream os = buildContext.newFileOutputStream(outFile)) {
properties.store(os, "Generated by camel-package-maven-plugin");
}
log.info("Generated " + outFile + " containing " + count + " Camel " + (count > 1 ? "dataformats: " : "dataformat: ") + names);
if (projectHelper != null) {
projectHelper.attachArtifact(project, "properties", "camelDataFormat", outFile);
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);